﻿using System;
using System.Collections.Generic;
using System.Linq;
using System.Text;
using System.Threading.Tasks;

// 给定一个包含销售数据的列表，每笔销售有销售员姓名和销售金额。按销售员姓名对销售记录进行分组，并计算每个销售员的总销售金额。
namespace CS_BEGINNER_TO_PROFICIENT.STD.LinqSTD
{
    public class P4
    {
        public static void Execute()
        {
            List<Sale> sales = new List<Sale>
            {
                new Sale { Name = "zhangsan", Amount = 200.0f },
                new Sale { Name = "lisi", Amount = 200.0f },
                new Sale { Name = "zhangsan", Amount = 200.0f },
                new Sale { Name = "lisi", Amount = 150.0f },
                new Sale { Name = "zhaowu", Amount = 200.0f },
            };

            var res = sales.GroupBy(sale => sale.Name)
                .Select(saleGroup => new { SalePeople = saleGroup.Key, TotalAmount = saleGroup.Sum(s => s.Amount)});

            foreach (var o in res)
            {
                Console.WriteLine($"SalePeople: {o.SalePeople} TotalAmount: {o.TotalAmount}");
            }
        }
    }

    public class Sale
    {
        public string Name { get; set; }
        public float Amount { get; set; }
    }
}
